using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;

namespace MyLibrary
{
    public class FileProcessor
    {
        public (int lineCount, int wordCount) GetFileStatistics(string filePath)
        {
            var lines = File.ReadAllLines(filePath);
            int lineCount = lines.Length;
            int wordCount = lines.Sum(line => CountWords(line));
            return (lineCount, wordCount);
        }

        public (int lineCount, int wordCount) FormatFile(string filePath)
        {
            var lines = File.ReadAllLines(filePath);
            var formattedLines = new List<string>();

            foreach (var line in lines)
            {
                var trimmedLine = line.Trim();
                if (!string.IsNullOrEmpty(trimmedLine) && !trimmedLine.StartsWith("//"))
                {
                    formattedLines.Add(trimmedLine);
                }
            }

            File.WriteAllLines(filePath, formattedLines);
            int lineCount = formattedLines.Count;
            int wordCount = formattedLines.Sum(line => CountWords(line));
            return (lineCount, wordCount);
        }

        public Dictionary<string, int> GetWordFrequency(string filePath)
        {
            var lines = File.ReadAllLines(filePath);
            var wordFrequency = new Dictionary<string, int>();

            foreach (var line in lines)
            {
                var words = Regex.Split(line, @"\W+").Where(word => !string.IsNullOrEmpty(word));
                foreach (var word in words)
                {
                    if (wordFrequency.ContainsKey(word))
                    {
                        wordFrequency[word]++;
                    }
                    else
                    {
                        wordFrequency[word] = 1;
                    }
                }
            }

            return wordFrequency;
        }

        private int CountWords(string line)
        {
            return Regex.Split(line, @"\W+").Count(word => !string.IsNullOrEmpty(word));
        }
    }
}